package ssm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.*;
import ssm.model.*;

import java.util.*;

@Service
public class TrackServiceImpl implements TrackService {

    @Autowired
    private TrackMapper trackMapper;
    @Autowired
    private SnapFaceMapper snapFaceMapper;

    public List<Track> get() {
        List<Track> tracks = trackMapper.get();
        return tracks;
    }

    public Map<String, Object> getByTime(String starttime, String endtime, int page, int pageNum) {
        int pageStart=(page)*pageNum;
        List<Track> tracks = trackMapper.getByTime(starttime, endtime, pageStart, pageNum);
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> list = toMap(tracks);
        result.put("Data", list);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        Long total = trackMapper.getTotal(map);
        result.put("total", total);
        return result;
    }


    public Map<String, Object> getByTimeOrJid(Map<String, Object> map) {
        //List<Track>tracks=trackMapper.getByTimeOrJid(map);
        Long time = System.currentTimeMillis();
        System.out.println(time);
        System.out.println(map);
        //List<YktBusiness> businesses = trackMapper.getBussines_limt(map);
        List<Track>tracks=trackMapper.getByTimeOrJid(map);
        System.out.println("track查询数据耗时:" + (System.currentTimeMillis() - time));
        time = System.currentTimeMillis();
        Long total = trackMapper.getTotal(map);
        System.out.println("total查询数据耗时:" + (System.currentTimeMillis() - time));
        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String,Object>>ts=toMap(tracks);
        result.put("data", ts);
        result.put("total", total);
        return result;
    }

    public List<Map<String, Object>> getByTable(String lsh, String ly) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("table", ly);
        map.put("lsh", lsh);
        List<Map<String, Object>> list = trackMapper.getByTable(map);

        return list;
    }

    public Map<String, Object> getResult_bs(Map<String, Object> param) {
        System.out.println("param:"+param);
        Long time=System.currentTimeMillis();
        Map<String,Object>result=new HashMap<String,Object>();
        List<Track>tracks=trackMapper.getResult_bs(param);
        System.out.println("查询data耗时:"+(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        List<Map<String,Object>>data=trackToBS(tracks);
        System.out.println("封装data耗时:"+(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        result.put("data",data);
        int total=trackMapper.getTotal_bs(param);
        System.out.println("查询总数量:"+(System.currentTimeMillis()-time));
        time=System.currentTimeMillis();
        result.put("total",total);
        return result;
    }

    public List<YktBusiness> getBusinessDetail(Map<String,Object> param) {
        List<YktBusiness> bs=trackMapper.getBusinessDetail(param);
        return bs;
    }

    public String getBigPicUrlByFace(String smartUrl) {
        String bigPicUrl=snapFaceMapper.getBigPicUrlByFace(smartUrl);
        return bigPicUrl;
    }

    private List<Map<String,Object>> trackToBS(List<Track> tracks) {
        List<Map<String,Object>>result=new ArrayList<Map<String, Object>>();
        for(Track track:tracks){
            Map<String,Object>map=new HashMap<String,Object>();
            map.put("id",track.getId());
            map.put("xm",track.getXm());
            map.put("xhgh",track.getXhgh());
            map.put("sksj",track.getSj());
            map.put("skdd",track.getDd());
            map.put("sklsh",track.getLsh());
            map.put("camerjd",track.getDevicejd());
            map.put("camerwd",track.getDevicewd());
            map.put("ly",track.getLy());
            map.put("sklx",track.getAction());
            map.put("lx",track.getLx());
            result.add(map);
        }
        return result;
    }

    public List<Map<String, Object>> toMap(List<Track> tracks) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        if(!tracks.isEmpty()){
            for (Track track : tracks) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", track.getXm());
                map.put("jid", track.getXhgh());
                map.put("passTime", track.getSj());
                map.put("position", track.getDd());
                map.put("type", track.getLx());
                map.put("zpzp", track.getZpzp());
                map.put("xsd", track.getXsd());
                map.put("camerJd", track.getDevicejd());
                map.put("camerWd", track.getDevicewd());
                map.put("action",track.getAction());
                map.put("ly",track.getLy());
                map.put("lsh",track.getLsh());
                list.add(map);
            }
        }
        return list;
    }
}
